<template>
    <div class="app-container">
        <el-row :gutter="10">
            <el-col :span="12" :offset="6">
                <div style="text-align: center;margin-bottom: 30px;font-size: 16px">
                    个人工作{{ createStatus ? '新增' : '更新' }}
                </div>
                <el-form :model="personalWorkForm" ref="personalWorkForm" label-width="100px" :rules="personalWorkFormRules">
                    <el-row :gutter="10">
                        <el-form-item label="所属主体" prop="enterpriseId" >
                            <el-select v-model="personalWorkForm.enterpriseId" placeholder="请选择主体" style="width: 100%" @change="getInUserList">
                                <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="职工" prop="useId" >
                            <el-select v-model="personalWorkForm.userId" placeholder="请选择职工" style="width: 100%">
                                <el-option v-for="(item,index) in userList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="工作简介" prop="remark">
                            <el-input type="text" v-model="personalWorkForm.remark" placeholder="请输入工作简介" clearable></el-input>
                        </el-form-item>
                          <el-form-item label="工作内容" prop="content">
                            <el-input  type="textarea"  :autosize="{ minRows: 3, maxRows: 5}"   maxlength="240"  show-word-limit v-model="personalWorkForm.content" placeholder="请输入工作内容" clearable></el-input>
                        </el-form-item>
                        <el-form-item label="工作开始时间" prop="startTime">
                            <el-date-picker
                                    v-model="personalWorkForm.startTime"
                                    value-format="yyyy-MM-dd "
                                    type="datetime"
                                    :picker-options="pickerOptionsStart"
                                    placeholder="选择开始时间"
                                    style="width: 100%">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="工作结束时间" prop="endTime">
                            <el-date-picker
                                    v-model="personalWorkForm.endTime"
                                    value-format="yyyy-MM-dd"
                                    type="datetime"
                                    placeholder="请选择结束时间"
                                    :picker-options="pickerOptionsEnd"
                                    style="width: 100%">
                            </el-date-picker>
                        </el-form-item>
                    </el-row>
                </el-form>
            </el-col>
        </el-row>
        <div slot="footer" style="text-align: center" class="dialog-footer">
            <el-button @click="cancelPersonalWork">取 消</el-button>
            <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createPersonalWork">创 建
            </el-button>
            <el-button :loading="addLoading" v-else type="primary" @click="updatePersonalWork">修 改</el-button>
        </div>
    </div>
</template>

<script>
import {getInPersonalWorkVo,addInPersonalWork,updateInPersonalWork} from "@/api/pesrsonWork";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {getInUserList} from "@/api/enterprise";


export default {
    name: "personWorkEdit",
    data() {
        return {
            personalWorkForm: {

            },
            addLoading: false,
            enterpriseDominantList:[],
            userList:[],
            personalWorkFormRules:{
                enterpriseId: [{required: true, message: '所属企业不能为空', trigger: 'blur'}],
                remark: [{required: true, message: '工作简介不能为空', trigger: 'blur'}],
                content: [{required: true, message: '工作内容不能为空', trigger: 'blur'}],
                startTime: [{required: true, message: '开始时间不能为空', trigger: 'blur'}],
                endTime: [{required: true, message: '结束时间不能为空', trigger: 'blur'}]
            },

        }
    },
    computed: {
        createStatus() {
            return this.$route.query.ucId == undefined
        },
        enterPriseHidden(){
            let roleCode = this.$store.getters.roleCode;
            return roleCode=="XT-SUPER"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"
        },
        pickerOptionsStart() {
            return {
                disabledDate: (time) => {
                    if (this.personalWorkForm.endTime) {
                        return time.getTime() > new Date(this.personalWorkForm.endTime).getTime()
                    }else{
                        return time.getTime() < Date.now() - 8.64e7
                    }
                }
            }
        },
        pickerOptionsEnd() {
            return {
                disabledDate: (time) => {
                    if(this.personalWorkForm.startTime){
                        return time.getTime() < new Date(this.personalWorkForm.startTime).getTime()
                    }else {
                        return time.getTime() < Date.now() - 8.64e7
                    }
                }
            }
        },
    },
    created() {
        this.getInEnterpriseDominantList()
        if (!this.createStatus) {
            let ucId = this.$route.query.ucId
            this.getInPersonalWorkVo(ucId)

        }
    },
    methods: {
        getInPersonalWorkVo(val) {
            getInPersonalWorkVo({ucId: val}).then(res => {
                this.personalWorkForm = res.data
                this.getInUserList(res.data.flagId)
            }).catch(error => {

            })
        },
        cancelPersonalWork() {
            window.history.back()
        },
        createPersonalWork() {
            this.addLoading = true
            this.$refs['personalWorkForm'].validate(valid => {
                if (valid) {
                    addInPersonalWork(this.personalWorkForm).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加个人工作成功"
                        })
                        this.cancelPersonalWork()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        updatePersonalWork() {
            this.addLoading = true
            this.$refs['personalWorkForm'].validate(valid => {
                if (valid) {
                    updateInPersonalWork(this.personalWorkForm).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新个人工作成功"
                        })
                        this.cancelPersonalWork()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        getInEnterpriseDominantList(){
            getInEnterpriseDominantList({deleted:false}).then(res=>{
                this.enterpriseDominantList = res.data
            }).catch(error=>{

            })
        },
        getInUserList(val){
            getInUserList({flagId:val,deleted:false}).then(res=>{
                this.userList = res.data
            }).catch(error=>{

            })
        },


    }

}
</script>
